﻿/**
 * 文字颜色和背景色插件
 */
(function(E){

E.plugin("ForeColor, BackColor", {
	menuTitle:'颜色面板',
	isNeedPanel:1,
	isNeedTitle:0,

	killEvents:['mousedown'],
	config: {
		//下拉面板的宽度和高度
		width:230,
		height:160,
		colors:[
			'000000,993300,333300,003300,003366,000080,333399,333333',
			'800000,FF6600,808000,008000,008080,0000FF,666699,808080',
			'FF0000,FF9900,99CC00,339966,33CCCC,3366FF,800080,999999',
			'FF00FF,FFCC00,FFFF00,00FF00,00FFFF,00CCFF,993366,C0C0C0',
			'FF99CC,FFCC99,FFFF99,CCFFCC,CCFFFF,99CCFF,CC99FF,FFFFFF'
		]
	},
	
	//当前选中的颜色，键值是eid
	selectedColor: {},
	
	init: function( self ){
		var html=[],a,span, colors, list = self.config['colors'];
		a='<a href="color:Transparent" onclick="return false;" '
			+'style="width:16px;height:16px;background:#000;"></a>';
		
		span='<span class="jqe-plugin-color" color="Transparent" style="width:206px;line-height:25px;">'
			+ a +'使用默认颜色</span>';
		html.push(span);
		for(var i=0; i<list.length; i++){
			colors = list[i].split(",");
			for(var j=0; j<colors.length; j++){
				a='<a href="color:#'+ colors[j] +'" style="background:#'+ colors[j] +'"  onclick="return false;"></a>';
				span='<span class="jqe-plugin-color" color="#'+ colors[j] +'">'+a+'</span>';
				html.push(span);
			}
		}
		
		self.cache['html'] = html.join('');
	},
	
	/**
	 * 设置颜色
	 */
	click: function( Panel, self ){
		var color = self.selectedColor[E.eid];
		
		if( !color ){
			//设置颜色默认值
			if( 'ForeColor' == self.name ){
				color = 'blue';
			}else{
				color = 'yellow';
			}
		}
		
		self.setColor( color );
		self.hide();
		return 1;
	},
	
	menu: function( Panel, self ){
		//设置面板
		Panel.set( self );
		return 1;
	},
	
	/**
	 * 点击颜色块时会触发此方法
	 */
	action: function( target, panel, self ){
		var name = self.name, color = target.closest('[color]').attr('color');
		//只有在点击颜色块时，才设置颜色并隐藏面板
		//其他点击忽略
		if( color ){
			self.selectedColor[E.eid] = color;
			$('#'+E.eid+'-'+name).find('.selected-color').css('background-color', color);
			self.setColor( color );
			E.monitor(name);
			self.hide();
		}
		// 终止事件，否则会导致打开jqe:#color错误链接
		return false;
	},
	
	//设置颜色
	setColor: function( color ){
		var self = this, name = self.name;
		color = color || 'Transparent';
		E.execCommand(name, false, color);
	}
});

})(jQEditor);